/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.rhythms.swiperecyclerview.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.rhythms.R;
import com.rhythms.swiperecyclerview.bean.RhythmsItemBean;
import com.rhythms.swiperecyclerview.listener.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by YOLANDA on 2016/7/22.
 */
public class RhythmsAdapterMainList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RhythmsItemBean> mRhythmsItemBeanList;

    private OnItemClickListener mOnItemClickListener;

    public RhythmsAdapterMainList(List<RhythmsItemBean> rhythmsItemBeanList) {
        mRhythmsItemBeanList = rhythmsItemBeanList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rhythms_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DefaultViewHolder defaultViewHolder = (DefaultViewHolder) holder;
        defaultViewHolder.setData(mRhythmsItemBeanList.get(position));
        defaultViewHolder.setOnItemClickListener(mOnItemClickListener);
    }

    @Override
    public int getItemCount() {
        return mRhythmsItemBeanList == null ? 0 : mRhythmsItemBeanList.size();
    }

    class DefaultViewHolder extends RecyclerView.ViewHolder {
        OnItemClickListener mOnItemClickListener;
        RhythmsItemBean mRhythmsItemBean;
        @BindView(R.id.rhythms_id_select)
        CheckBox rhythmsIdSelect;
        @BindView(R.id.rhythms_id_name)
        TextView rhythmsIdName;
        @BindView(R.id.rhythms_id_remarks)
        TextView rhythmsIdRemarks;
        @BindView(R.id.rhythms_id_play)
        ToggleButton rhythmsIdPlay;

        public DefaultViewHolder(View itemView) {
            super(itemView);
//            LayoutInflater.from(mPresenter.mContext).inflate(R.layout.rhythms_item, null);
            ButterKnife.bind(this, itemView);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        public void setData(RhythmsItemBean rhythmsItemBean) {
            mRhythmsItemBean = rhythmsItemBean;
//            this.tvTitle.setText(title);
            // 如果可以编辑
            rhythmsIdName.setText(rhythmsItemBean.getRhyItemShow().getRhyName());
            rhythmsIdRemarks.setText(rhythmsItemBean.getRhyItemShow().getRhyRemarks());

            rhythmsIdSelect.setChecked(rhythmsItemBean.isRhySelect());
            rhythmsIdPlay.setChecked(rhythmsItemBean.isRhyPlaying());
        }

        @OnClick({R.id.rhythms_id_main_item, R.id.rhythms_id_select, R.id.rhythms_id_play, R.id.rhythms_id_delete})
        public void onViewClicked(View view) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition(), view);
            }
        }

        @OnCheckedChanged({R.id.rhythms_id_select, R.id.rhythms_id_play})
        public void onButtonCheck(CompoundButton view, boolean ischanged) {
//            if (mOnItemClickListener != null) {
//                mOnItemClickListener.onItemClick(getAdapterPosition(), view, ischanged);
//            }
            switch (view.getId()) {
                case R.id.rhythms_id_select:
                    mRhythmsItemBean.setRhySelect(ischanged);
                    break;
            }
        }
    }
}
